﻿using System;
using System.Collections.Generic;
using System.Linq;
using Game.Foundation;
using MF.Networker;

namespace Game.Server.ChineseChess
{
    class GameRoomServer : GameRoomConfig
    {
        private GameAreaServer _area;
        private Dictionary<string, Player> _managerDict;
        private List<GameGroup> _deskList;

        public int AreaID
        {
            get
            {
                return this._area.ID;
            }
        }

        public int DeskCount
        {
            get
            {
                return this._deskList.Count;
            }
        }

        public GameRoomServer(GameAreaServer area)
        {
            this._area = area;
            this._managerDict = new Dictionary<string, Player>();
            this._deskList = new List<GameGroup>();
        }

        public GameGroup GetDesk(int deskId)
        {
            return this._deskList.SingleOrDefault(d => d.ID == deskId);
        }

        public void RegisterDesk(GameGroup desk)
        {
            if (desk == null) return;

            this._deskList.Add(desk);
            desk.NewGameStarted += new EventHandler<Game.Foundation.NewGameEventArgs>(desk_NewGameStarted);
        }

        void desk_NewGameStarted(object sender, Game.Foundation.NewGameEventArgs e)
        {
            Packet p = new Packet((int)PacketType.游戏开始通知);
            p.WriteInt32(e.GroupID);
            p.WriteString(e.BlackCampUserName);
            p.WriteString(e.RedCampUserName);

            SendToAll(p);
        }

        public void SendToAll(Packet p)
        {
            //lock (m_dictMutex)
            //{
            foreach (var item in this._managerDict)
            {
                //ClientStatus status = item.Value.GetRelationData<ClientStatus>();
                //if (status == ClientStatus.Work)
                //{
                item.Value.SendMessage(p);
                //}
            }
        }

        public void AddUser(Player player)
        {
            //进来先告诉你这里的情况
            player.SendMessage(this.CreateGameGroupListPacket());

            this._managerDict[player.UserName] = player;
            Console.WriteLine(string.Format("玩家[{0}]进入[{1}][{2}]", player.UserName, this._area.Name, this.Name));
            Packet p = new Packet();
            p.Protocol = (int)PacketType.进入房间通知;
            p.WriteString(player.UserName);
            this.SendToAll(p);
        }

        public bool ContainsUser(string userName)
        {
            return this._managerDict.ContainsKey(userName);
        }

        public Packet CreateGameGroupListPacket()
        {
            Packet p = new Packet((int)PacketType.分组列表返回包);
            p.WriteInt32(this._deskList.Count);
            foreach (GameGroup group in this._deskList)
            {
                p.WriteInt32(group.ID);
                p.WriteBytes(group.GetBytes());
            }

            return p;
        }
    }
}
